package com.lianjing.sh.acceleo.controller.tool.service;

import java.util.List;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.ocl.util.CollectionUtil;
import org.eclipse.uml2.uml.DirectedRelationship;
import org.eclipse.uml2.uml.Element;

public class Service_Common{
	
	public List<Element> getInsThrough(Element type, Object inType,Object throughType){
		List<Element> newElements = CollectionUtil.createNewSequence(); 
		for(DirectedRelationship ship : type.getTargetDirectedRelationships()){
			if(((EClass)throughType).getInstanceClass().isInstance(ship)){
				for(Element source : ship.getSources()){
					if(((EClass)inType).getInstanceClass().isInstance(source)){
						newElements.add(source);
					}
				}
			}
		}
		return newElements;
	}
	
	public List<Element> getIns(Element type, Object inType){
		List<Element> newElements = CollectionUtil.createNewSequence(); 
		for(DirectedRelationship ship : type.getTargetDirectedRelationships()){
			for(Element source : ship.getSources()){
				if(((EClass)inType).getInstanceClass().isInstance(source)){
					newElements.add(source);
				}
			}
		}
		return newElements;
	}
	
	public Element getInThrough(Element type, Object inType,Object throughType){
		for(DirectedRelationship ship : type.getTargetDirectedRelationships()){
			if(((EClass)throughType).getInstanceClass().isInstance(ship)){
				for(Element source : ship.getSources()){
					if(((EClass)inType).getInstanceClass().isInstance(source)){
						return source;
					}
				}
			}
		}
		return null;
	}
	
	public Element getIn(Element type, Object inType){
		for(DirectedRelationship ship : type.getTargetDirectedRelationships()){
			for(Element source : ship.getSources()){
				if(((EClass)inType).getInstanceClass().isInstance(source)){
					return source;
				}
			}
		}
		return null;
	}
	
	public List<Element> getOutsThrough(Element type, Object outType,Object throughType){
		List<Element> newElements = CollectionUtil.createNewSequence(); 
		for(DirectedRelationship ship : type.getSourceDirectedRelationships()){
			if(((EClass)throughType).getInstanceClass().isInstance(ship)){
				for(Element source : ship.getTargets()){
					if(((EClass)outType).getInstanceClass().isInstance(source)){
						newElements.add(source);
					}
				}
			}
		}
		return newElements;
	}
	
	public List<Element> getOuts(Element type, Object outType){
		List<Element> newElements = CollectionUtil.createNewSequence(); 
		for(DirectedRelationship ship : type.getSourceDirectedRelationships()){
			for(Element source : ship.getTargets()){
				if(((EClass)outType).getInstanceClass().isInstance(source)){
					newElements.add(source);
				}
			}
		}
		return newElements;
	}
	
	public Element getOutThrough(Element type, Object outType,Object throughType){
		for(DirectedRelationship ship : type.getSourceDirectedRelationships()){
			if(((EClass)throughType).getInstanceClass().isInstance(ship)){
				for(Element source : ship.getTargets()){
					if(((EClass)outType).getInstanceClass().isInstance(source)){
						return source;
					}
				}
			}
		}
		return null;
	}
	
	public Element getOut(Element type, Object outType){
		for(DirectedRelationship ship : type.getSourceDirectedRelationships()){
			for(Element source : ship.getTargets()){
				if(((EClass)outType).getInstanceClass().isInstance(source)){
					return source;
				}
			}
		}
		return null;
	}
	
}
